﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace SwormTests.IUD
{
    public class unconventional_pks : TestBase
    {
        public unconventional_pks(DbToTest type) : base(type) { }

        class unconventional_pk
        {
            public long id { get; set; } // this is not the PK
            public string actual_pk { get; set; }
            public string string1 { get; set; }
        }

        protected override Sworm.TypeAnalyzer CreateTypeAnalyzer()
        {
            return new TestMetadata();
        }

        class TestMetadata : Sworm.TypeAnalyzer
        {
            public override Sworm.ColumnData SelectPrimaryKey(Sworm.TableData table, out bool isAutoIncrement)
            {
                isAutoIncrement = false;

                if (table.ClassType == typeof(unconventional_pk))
                {
                    return table.Columns.Single(c => c.Member.Name == "actual_pk");
                }
                return base.SelectPrimaryKey(table, out isAutoIncrement);
            }
        }

        [Test]
        public void insert_and_update()
        {
            var table = MetadataBuider.TableDataFor(typeof(unconventional_pk));
            Assert.AreEqual("actual_pk", table.OptionalData.PrimaryKey.Member.Name);

            // insert
            var item1 = new unconventional_pk() { id = 23, actual_pk = "'first", string1 = "'hello" };
            var item2 = new unconventional_pk() { id = 45, actual_pk = "'second", string1 = "'TWO" };
            DB.Insert(item1);
            DB.Insert(item2);

            // read
            var fromDB = DB.Query.Joining<unconventional_pk>("u").List();
            item1 = fromDB.Single(i => i.actual_pk == item1.actual_pk);
            item2 = fromDB.Single(i => i.actual_pk == item2.actual_pk);

            Assert.AreEqual(23, item1.id);
            Assert.AreEqual("'first", item1.actual_pk);
            Assert.AreEqual("'hello", item1.string1);

            Assert.AreEqual(45, item2.id);
            Assert.AreEqual("'second", item2.actual_pk);
            Assert.AreEqual("'TWO", item2.string1);

            // update
            item1.id = 99;
            item1.string1 = null;

            item2.id = 22;
            item2.string1 = " % ";

            DB.Update(item1);
            DB.Update(item2);

            // read
            fromDB = DB.Query.Joining<unconventional_pk>("u").List();
            item1 = fromDB.Single(i => i.actual_pk == item1.actual_pk);
            item2 = fromDB.Single(i => i.actual_pk == item2.actual_pk);

            Assert.AreEqual(99, item1.id);
            Assert.AreEqual("'first", item1.actual_pk);
            Assert.IsNull(item1.string1);

            Assert.AreEqual(22, item2.id);
            Assert.AreEqual("'second", item2.actual_pk);
            Assert.AreEqual(" % ", item2.string1);
        }
    }
}
